package com.upcode.weighing.admin.controller.sys;

import com.upcode.weighing.base.controller.BaseController;
import com.upcode.weighing.log.OpsLog;
import com.upcode.weighing.service.SysResService;
import com.upcode.weighing.service.SysRoleResService;
import com.upcode.weighing.vo.add.SysResAddVo;
import com.upcode.weighing.vo.del.BaseDelVo;
import com.upcode.weighing.vo.edit.ButtonEditVo;
import com.upcode.weighing.vo.edit.SysResEditVo;
import com.upcode.weighing.vo.req.PageReqVo;
import com.upcode.weighing.vo.resp.pc.SysResVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import static com.upcode.weighing.common.constant.BillConstants.ZI_YUAN_TYPE_CAIDAN;

@RestController
@RequestMapping(value = "/res")
@Api(value = "/res",description = "菜单管理接口",produces = MediaType.ALL_VALUE,tags = {"菜单管理接口"})
public class SysResController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(SysResController.class);

    @Autowired
    private SysResService sysResService;
    @Autowired
    private SysRoleResService sysRoleResService;

    @OpsLog(operation = "菜单管理/获取左侧菜单树/查询" ,type = 1)
    @ApiOperation(value = "获取左侧菜单树",notes ="")
    @GetMapping("/getLeftRes")
    public Object getRes(){ return success(sysResService.getLeftRes()); }

    @OpsLog(operation = "菜单管理/添加菜单/新增" ,type = 2)
    @ApiOperation(value = "添加菜单",notes ="")
    @PostMapping("/addRes")
    public Object addRes(@RequestBody SysResAddVo vo){
        if(sysResService.checkRes(vo.getPermission())>0){
            return error("该资源编码已存在!");
        }
        if(sysResService.checkVueKey(vo.getVueKey())>0){
            return error("该标识已存在!");
        }
        vo.setType(ZI_YUAN_TYPE_CAIDAN);//类型为菜单
        if(vo.getPid()==null){
            //父级ID为空则表示添加一级菜单
            vo.setPid(0L);//一级菜单的父级ID为0
            int i=sysResService.addRes(vo);
            if(i>0){
                return success("添加成功");
            }
        }else{
            //父级ID不为空则表示添加二级菜单
            if(sysResService.checkChild(vo.getPid())==0){
                int i=sysResService.addRes(vo);
                if(i>0){
                    return success("添加成功");
                }
            }else{
                return success("不允许再添加子菜单");
            }
        }
        return error("添加失败，请联系超级管理员");
    }


    @OpsLog(operation = "菜单管理/获取要修改的菜单信息/查询" ,type = 1)
    @ApiOperation(value = "获取要修改的菜单信息",notes ="")
    @GetMapping("/getEditRes")
    public Object getEditRes(Long id){
        return success(sysResService.getEditRes(id));
    }

    @OpsLog(operation = "菜单管理/修改菜单/修改" ,type = 4)
    @ApiOperation(value = "修改菜单",notes ="")
    @PostMapping("/editRes")
    public Object editRes(@RequestBody SysResEditVo vo){
        if(sysResService.checkRes(vo.getPermission())>1){
            return error("编码已存在!");
        }
        if(sysResService.checkVueKey(vo.getVueKey())>1){
            return error("该标识已存在!");
        }
        int i=sysResService.editRes(vo);
        if(i>0){
            return success("修改成功");
        }
        return error("修改失败，请联系系统管理员");
    }

    @OpsLog(operation = "菜单管理/删除菜单/删除" ,type = 3)
    @ApiOperation(value = "删除菜单",notes ="")
    @PostMapping("/delRes")
    public Object delRes(@RequestBody BaseDelVo vo){
        //解除菜单与角色的绑定
        int i=sysRoleResService.delResRole(vo);
        //删除菜单
        int j=sysResService.delRes(vo);
        if(j>0){
            return success("删除成功");
        }
        return error("删除失败，请联系系统管理员");
    }

    @OpsLog(operation = "菜单管理/菜单管理分页列表/查询" ,type = 1)
    @ApiOperation(value = "菜单管理分页列表",notes ="")
    @PostMapping("/selectPageList")
    public Object selectPageList(@RequestBody PageReqVo<SysResVo> vo){
        return success(sysResService.selectPageList(vo));
    }

    @OpsLog(operation = "菜单管理/获取一级菜单列表/查询" ,type = 1)
    @ApiOperation(value = "获取一级菜单列表",notes ="")
    @GetMapping("/getParentResList")
    public Object getParentResList(){
        return success(sysResService.getParentResList());
    }

    @OpsLog(operation = "菜单管理/根据一级菜单获取二级菜单/查询" ,type = 1)
    @ApiOperation(value = "根据一级菜单获取二级菜单",notes ="")
    @GetMapping("/getChildList")
    public Object getChildList(Long pId){
        return success(sysResService.getChildList(pId));
    }

    @OpsLog(operation = "菜单管理/添加按钮/新增" ,type = 2)
    @ApiOperation(value = "添加按钮",notes ="")
    @PostMapping("/addButton")
    public Object addButton(@RequestBody SysResAddVo vo){
        if(sysResService.checkRes(vo.getPermission())>0){
            return error("编码已存在!");
        }
        if(sysResService.checkVueKey(vo.getVueKey())>0){
            return error("该标识已存在!");
        }
        int i=sysResService.addButton(vo);
        if(i>0){
            return success("添加成功");
        }
        return error("添加失败");
    }

    @OpsLog(operation = "菜单管理/获取按钮信息/查询" ,type = 1)
    @ApiOperation(value = "获取按钮信息",notes ="")
    @GetMapping("/getButton")
    public Object getButton(Long id){
        return success(sysResService.getButton(id));
    }

    @OpsLog(operation = "菜单管理/修改按钮/修改" ,type = 4)
    @ApiOperation(value = "修改按钮",notes ="")
    @PostMapping("/editButton")
    public Object editButton(@RequestBody ButtonEditVo vo){
       if(sysResService.checkRes(vo.getPermission())>1){
           return error("编码已存在");
       }
        if(sysResService.checkVueKey(vo.getVueKey())>1){
            return error("该标识已存在!");
        }
       int i=sysResService.updateButton(vo);
       if(i>0){
           return success("修改成功");
       }
        return error("修改失败");
    }

    @OpsLog(operation = "菜单管理/删除按钮/删除" ,type = 3)
    @ApiOperation(value = "删除按钮",notes ="")
    @PostMapping("/delButton")
    public Object delButton(@RequestBody BaseDelVo vo){
        sysRoleResService.delResRole(vo);
        int j=sysResService.delButton(vo);
        if(j>0){
            return success("删除成功");
        }
        return error("删除失败");
    }
}
